package com.wust.tieshi;

import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wust.R;
import com.wust.baby.Baby;
import com.wust.baby.BabyDao;
import com.wust.baby.BabyInfo;
import com.wust.baby.BabyInfoDao;
import com.wust.menu.SecondGroupTab;
import com.wust.mode.Mode;
import com.wust.mode.ModeDao;
import com.wust.util.SqltimeToTime;

public class AutoCheckActivity extends Activity{
	private BabyInfoDao infoDao;
	private String baby_date;
	private Date birth;
	private SqltimeToTime timeTransfer;
	private TextView sexview;
	private TextView ageview;
	private TextView heightview;
	private TextView weightview;
	private TextView bustview;
	private TextView headwidthview;
	private TextView sexview2;
	private TextView ageview2;
	private TextView heightview2;
	private TextView weightview2;
	private TextView bustview2;
	private TextView headwidthview2;
	private List<BabyInfo>infolist;
	private ImageView imageView;
	private Button subbtn;
	private Button rstbtn;
	private LinearLayout layout;
	private String baby_id;
	private BabyInfo info;
	private Baby baby;
	private BabyDao babyDao;
	private ModeDao modeDao;
	private Mode mode;
	private String user_name;
	private long age;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.autocheck);
		layout=(LinearLayout) findViewById(R.id.layout_2);
		ageview=(TextView)findViewById(R.id.check_age);
		sexview=(TextView)findViewById(R.id.check_sex);
		heightview=(TextView) findViewById(R.id.check_height);
		weightview=(TextView) findViewById(R.id.check_weigth);
		bustview=(TextView) findViewById(R.id.check_bust);
		headwidthview=(TextView) findViewById(R.id.check_headwidth);
		ageview2=(TextView)findViewById(R.id.check_age2);
		sexview2=(TextView)findViewById(R.id.check_sex2);
		heightview2=(TextView) findViewById(R.id.check_height2);
		weightview2=(TextView) findViewById(R.id.check_weigth2);
		bustview2=(TextView) findViewById(R.id.check_bust2);
		headwidthview2=(TextView) findViewById(R.id.check_headwidth2);
		imageView=(ImageView)findViewById(R.id.result_img);
		timeTransfer=new SqltimeToTime();
		rstbtn=(Button)findViewById(R.id.auto_check_back);
		
		subbtn=(Button)findViewById(R.id.auto_check_ok);
		subbtn.setOnClickListener(new CheckBabyListener());
		rstbtn.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(AutoCheckActivity.this, ViewTestActivity.class);  
		        // 把Activity转换成一个Window，然后转换成View  
		        Window w = SecondGroupTab.group.getLocalActivityManager()  
		                .startActivity("ViewTestActivity",intent);  
		        View view = w.getDecorView();  
		        //设置要跳转的Activity显示为本ActivityGroup的内容  
		        SecondGroupTab.group.setContentView(view);  
			}
			
		});
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		init();
		super.onResume();
	}
	@Override  
    public void onBackPressed() {   
        //把后退事件交给子Activity处理  
		Intent intent = new Intent(AutoCheckActivity.this, ViewTestActivity.class);  
        // 把Activity转换成一个Window，然后转换成View  
        Window w = SecondGroupTab.group.getLocalActivityManager()  
                .startActivity("ViewTestActivity",intent);  
        View view = w.getDecorView();  
        //设置要跳转的Activity显示为本ActivityGroup的内容  
        SecondGroupTab.group.setContentView(view);  
       
    } 
	private void init()
	{
		SharedPreferences preferences = getSharedPreferences("userinfo",0);
		baby_id=preferences.getString("baby_id", "");
		user_name=preferences.getString("user_name", "");
		baby_date=preferences.getString("baby_date","");
		infoDao=new BabyInfoDao(this);
		babyDao=new BabyDao(this);
		infolist=infoDao.getBabyInfoListById(baby_id);
		baby=babyDao.getBabyByUserName(user_name);
		info=infolist.get(infolist.size()-1);
		sexview.setText(baby.getBaby_sex());
		birth=timeTransfer.stringToDate(baby_date, "yyyy-MM-dd");
		Date date=timeTransfer.stringToDate(info.getInfo_date(),"yyyy-MM-dd");
		
		age=(date.getTime()-birth.getTime())/60/60/24/30/1000;
		ageview.setText(String.valueOf((int)age)+"月");
		heightview.setText(String.valueOf(info.getInfo_height())+"cm");
		weightview.setText(String.valueOf(info.getInfo_weigth())+"kg");
		bustview.setText(String.valueOf(info.getInfo_bust())+"cm");
		headwidthview.setText(String.valueOf(info.getInfo_circumference())+"kg");
	}
	private void checkstandard()
	{
		modeDao=new ModeDao(this);
		mode=modeDao.getModeInfo(baby.getBaby_sex(),String.valueOf(age));
		ageview2.setText(String.valueOf(mode.getMode_age())+"月");
		heightview2.setText(String.valueOf(mode.getMode_height())+"cm");
		weightview2.setText(String.valueOf(mode.getMode_width())+"kg");
		bustview2.setText(String.valueOf(mode.getMode_bust()+"cm"));
		headwidthview2.setText(String.valueOf(mode.getMode_headwidth())+"cm");
		sexview2.setText(mode.getMode_sex());
		imageView.setVisibility(View.VISIBLE);
		boolean checkheight=checkHeight((int)age,info.getInfo_height());
		boolean checkweigth=checkWeigth((int)age,info.getInfo_weigth());
		rstbtn.setVisibility(View.VISIBLE);
		subbtn.setVisibility(View.GONE);
		if(checkheight==false||checkweigth==false)
			imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.check_result2));
	}
	private boolean checkHeight(int age,double height)
	{
		boolean flag=false;
		switch(age)
		{
		case 0 :
			if(height>=48.2&&height<=52.8)
				flag=true;
			break;
		case 1:
			if(height>=52.1&&height<=57.0)
				flag=true;
			break;
		case 2:
			if(height>=55.5&&height<=60.7)
				flag=true;
			break;
		case 3:
			if(height>=58.5&&height<=63.7)
				flag=true;
			break;
		case 4:
			if(height>=61.0&&height<=66.4)
				flag=true;
			break;
		case 5:
			if(height>=63.2&&height<=68.6)
				flag=true;
			break;
		case 6:
			if(height>=65.1&&height<=70.5)
				flag=true;
			break;
		case 7:
			if(height>=66.2&&height<=71.3)
				flag=true;
			break;
		case 8:
			if(height>=68.3&&height<=73.6)
				flag=true;
			break;
		case 9:
			if(height>=70.2&&height<=75.4)
				flag=true;
			break;
		case 10:
			if(height>=71.0&&height<=76.3)
				flag=true;
			break;
		case 11:
			if(height>=72.1&&height<=77.5)
				flag=true;
			break;
		case 12:
			if(height>=73.4&&height<=78.8)
				flag=true;
			break;
		default:
			flag=false;
			break;
		}
		return flag;
	}
	private boolean checkWeigth(int age,double weigth)
	{
		boolean flag=false;
		switch(age)
		{
		case 0 :
			if(weigth>=2.9&&weigth<=3.8)
				flag=true;
			break;
		case 1:
			if(weigth>=3.6&&weigth<=5.0)
				flag=true;
			break;
		case 2:
			if(weigth>=4.3&&weigth<=6.0)
				flag=true;
			break;
		case 3:
			if(weigth>=5.0&&weigth<=6.9)
				flag=true;
			break;
		case 4:
			if(weigth>=5.7&&weigth<=7.6)
				flag=true;
			break;
		case 5:
			if(weigth>=6.3&&weigth<=8.2)
				flag=true;
			break;
		case 6:
			if(weigth>=6.9&&weigth<=8.8)
				flag=true;
			break;
		case 7:
			if(weigth>=7.3&&weigth<=9.2)
				flag=true;
			break;
		case 8:
			if(weigth>=7.8&&weigth<=9.8)
				flag=true;
			break;
		case 9:
			if(weigth>=8.2&&weigth<=10.2)
				flag=true;
			break;
		case 10:
			if(weigth>=8.6&&weigth<=10.6)
				flag=true;
			break;
		case 11:
			if(weigth>=8.8&&weigth<=10.8)
				flag=true;
			break;
		case 12:
			if(weigth>=9.1&&weigth<=11.3)
				flag=true;
			break;
		default:
			flag=false;
			break;
		}
		return flag;
	}
	class CheckBabyListener implements OnClickListener
	{

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			checkstandard();
			layout.setVisibility(layout.VISIBLE);
		}
		
	}
}
